package spring.main;

import java.io.IOException;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.TwitterApi;
import org.scribe.model.Token;
import org.scribe.oauth.OAuthService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ventana.Verificacion;
import ventana.Verificacion;

public class Main {
    public static void main(String[] args) {
        //ApplicationContext ctx = new ClassPathXmlApplicationContext("/context.xml");
        //JFrame v = ctx.getBean("ventanita", JFrame.class);
        //v.setVisible(true);

        OAuthService service = new ServiceBuilder()
                               .provider(TwitterApi.class)
                               .apiKey("BUb0lJE80kD5eM2C7xVdLw")
                               .apiSecret("0reVv7jE1vm516jYY2TZflS1qoY9u46s6J6bOnpnt4")
                               .build();
        Token requestToken = service.getRequestToken();
        String authUrl = service.getAuthorizationUrl(requestToken);

        Verificacion ventana = new Verificacion(requestToken,service);
        ventana.setVisible(true);

        openURL(authUrl);

    }

    public static void openURL(String url) {
        String osName = System.getProperty("os.name");
        try {
            if (osName.startsWith("Windows")) {
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
            } else if (osName.startsWith("Mac OS X")) {
                // Runtime.getRuntime().exec("open -a safari " + url);
                // Runtime.getRuntime().exec("open " + url + "/index.html");
                Runtime.getRuntime().exec("open " + url);
            } else {
                System.out.println("Please open a browser and go to "+ url);
            }
        } catch (IOException e) {
            System.out.println("Failed to start a browser to open the url " + url);
            e.printStackTrace();
        }
    }
}
